package com.example.user3.demo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatImageView;

public class OvalImageView extends AppCompatImageView {
    private int size;
    private int heightsize1;
    /*
     * 弧形高度
     */
    private int mArcHeight;
    private static final String TAG = "ArcImageView";

    private Paint mPaint;

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
        invalidate();
    }

    public int color= Color.parseColor("#DC5A40") ;
    public OvalImageView(Context context) {
        super(context);
    }

    public OvalImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWillNotDraw(false);
    }

    public OvalImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        size = MeasureSpec.getSize(widthMeasureSpec);
        heightsize1 = MeasureSpec.getSize(heightMeasureSpec);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        mArcHeight = getHeight();
        Path path = new Path();
        path.moveTo(0, 0);
        path.lineTo(0, getHeight() - getHeight() /4);
        path.quadTo(getWidth() / 2, getHeight()+getHeight()/8, getWidth(), getHeight() - getHeight() / 4);
        path.lineTo(getWidth() , 0);
        path.close();
//        canvas.drawPath(path, mPaint);// 绘制Dst
        canvas.clipPath(path);
        super.onDraw(canvas);

//        init( canvas);
    }
    private void init(Canvas canvas){
        int rectHeight=getHeight()- DisplayUtil.dp2px(getContext(),50);
        Paint p = new Paint();
        p.setColor(color);
        RectF rect = new RectF(0, 0, getWidth(), rectHeight);
        canvas.drawRect(rect,p);
        p.setColor(color);
        p.setAntiAlias(true);
        RectF rectF = new RectF(0, getHeight()-DisplayUtil.dp2px(getContext(),50)*2, getWidth(), getHeight());
        canvas.drawArc(rectF, 0, 180, true, p);
    }

}